<template>
    <div>
        <div class="imgbackground">
            <img class="avatar" :src="userDetail.userAvatar" alt="avatar" />
            <div class="name">
                {{ userDetail.nick }}
            </div>
            <el-tooltip
                class="item"
                effect="dark"
                content="编辑签名"
                placement="bottom"
            >
                <div class="signature" @click="_editUserInfo">
                    {{ userDetail.signature }}
                </div>
            </el-tooltip>
            <el-row>
                <el-col class="describe" :span="4"><span>作品</span></el-col>
                <el-col class="describe" :span="5"><span>收藏</span></el-col>
                <el-col class="describe" :span="6"
                    ><span @click="_showFollowrdInfo">关注</span></el-col
                >
                <el-col class="describe" :span="5"
                    ><span @click="_showFanInfo">粉丝</span></el-col
                >
                <el-col class="describe" :span="4"><span>被赞</span></el-col>
            </el-row>
            <el-row>
                <el-col class="describe_title" :span="4"
                    ><span>{{ userDetail.photoCounts }}</span></el-col
                >
                <el-col class="describe_title" :span="5"
                    ><span>{{ userDetail.collectionCounts }}</span></el-col
                >
                <el-col class="describe_title" :span="6"
                    ><span @click="_showFollowrdInfo">{{
                        userDetail.followCounts
                    }}</span></el-col
                >
                <el-col class="describe_title" :span="5"
                    ><span @click="_showFanInfo">{{
                        userDetail.fanCounts
                    }}</span></el-col
                >
                <el-col class="describe_title" :span="4"
                    ><span>{{ userDetail.approvalCounts }}</span></el-col
                >
            </el-row>
        </div>
        <div class="userInfo">
            <el-tabs
                type="border-card"
                :stretch="true"
                @tab-click="_handleClick"
            >
                <el-tab-pane label="我的作品">
                    <span slot="label">我的作品</span>
                    <el-card class="container">
                        <transition name="el-fade-in-linear">
                            <el-row v-show="workShow">
                                <el-col
                                    :span="8"
                                    v-for="(item, index) in worksDetail"
                                    :key="index"
                                >
                                    <el-card
                                        class="box-card"
                                        :body-style="{
                                            margin: '5px',
                                            padding: '0px',
                                        }"
                                    >
                                        <img
                                            :src="item.link"
                                            class="image"
                                            lazy
                                        />
                                        <div style="padding: 14px">
                                            <div class="bottom clearfix">
                                                <el-button
                                                    type="text"
                                                    class="button"
                                                    @click.native="
                                                        _openPhotoDetail(
                                                            item.photoId
                                                        )
                                                    "
                                                    >查看</el-button
                                                >
                                                <el-button
                                                    type="text"
                                                    class="button"
                                                    @click="
                                                        _deleteWork(
                                                            item.photoId
                                                        )
                                                    "
                                                    >删除</el-button
                                                >
                                            </div>
                                        </div>
                                    </el-card></el-col
                                >
                            </el-row>
                        </transition>
                        <el-button @click="_toLastPhotos">上一页</el-button>
                        <el-button @click="_toNextPhotos">下一页</el-button>
                    </el-card>
                </el-tab-pane>
                <el-tab-pane label="我的评论">
                    <span slot="label">我的评论</span>
                    <el-card class="container">
                        <transition name="el-fade-in-linear">
                            <div v-show="workShow">
                                <el-card
                                    v-show="workShow"
                                    class="comment_card"
                                    v-for="(item, index) in commentList"
                                    :key="index"
                                >
                                    <img
                                        class="comment_img"
                                        :src="item.link"
                                        @click="_openPhotoDetail(item.photoId)"
                                    />
                                    <span class="comment_title">{{
                                        item.title
                                    }}</span>
                                    <div class="comment_main">
                                        <span class="comment_content"
                                            >内容：{{ item.content }}</span
                                        >
                                        <span class="comment_time"
                                            >时间：{{ item.commentTime }}</span
                                        >
                                        <span class="comment_approval"
                                            >获赞数量：{{
                                                item.commentApproval
                                            }}</span
                                        >
                                        <el-button
                                            class="comment_delete"
                                            type="danger"
                                            @click="
                                                _deleteComment(item.photoId)
                                            "
                                            >删除</el-button
                                        >
                                    </div>
                                </el-card>
                            </div>
                        </transition>
                        <el-button @click="_toLastReplyList">上一页</el-button>
                        <el-button @click="_toNextReplyList">下一页</el-button>
                    </el-card></el-tab-pane
                >
                <el-tab-pane label="我的收藏">
                    <span slot="label">我的收藏</span>
                    <el-card class="container">
                        <transition name="el-fade-in-linear">
                            <div v-show="workShow">
                                <el-card
                                    v-for="(item, index) in collectionList"
                                    :key="index"
                                >
                                    <img
                                        class="comment_img"
                                        :src="item.link"
                                        @click="_openPhotoDetail(item.photoId)"
                                    />
                                    <span class="comment_title">{{
                                        item.title
                                    }}</span>
                                    <div class="comment_main">
                                        <el-button
                                            class="comment_delete"
                                            type="danger"
                                            @click="
                                                _cancelCollection(item.photoId)
                                            "
                                            >取消收藏</el-button
                                        >
                                    </div>
                                </el-card>
                            </div>
                        </transition>
                        <el-button @click="_toLastCollectionList"
                            >上一页</el-button
                        >
                        <el-button @click="_toNextCollectionList"
                            >下一页</el-button
                        >
                    </el-card></el-tab-pane
                >
            </el-tabs>
        </div>
        <!-- 编辑资料的抽屉 -->
        <el-drawer
            width="30%"
            size="70%"
            :visible.sync="editVisible"
            direction="ttb"
            :before-close="_handleClose"
        >
            <div class="editBox">
                <el-row>
                    <el-col :span="12">
                        <div @click="_changeAvatar">
                            <el-tooltip
                                class="item"
                                effect="dark"
                                content="更换新头像"
                                placement="bottom"
                            >
                                <el-avatar
                                    class="avatar"
                                    :size="150"
                                    :src="editForm.userAvatar"
                                ></el-avatar>
                            </el-tooltip>
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <el-card style="width: 70%">
                            <el-form
                                :model="editForm"
                                ref="editForm"
                                :rules="rules"
                            >
                                <el-form-item label="昵称" prop="nick">
                                    <el-input
                                        v-model="editForm.nick"
                                        placeholder="请输入昵称"
                                    ></el-input>
                                </el-form-item>
                                <el-form-item label="签名" prop="signature">
                                    <el-input
                                        v-model="editForm.signature"
                                        placeholder="请输入签名"
                                    ></el-input>
                                </el-form-item>
                            </el-form>
                        </el-card>
                    </el-col>
                </el-row>
                <el-row style="margin-top: 20px">
                    <el-button type="primary" @click="_editUser"
                        >确定</el-button
                    >
                </el-row>
            </div>
        </el-drawer>
        <el-dialog title="选择新头像" :visible.sync="avatarChooseBoxVisible">
            <el-card class="avatarContain">
                <el-row>
                    <el-col :span="6" v-for="item in avatarList" :key="item.id">
                        <el-image
                            class="avataritem"
                            :src="item.url"
                            lazy
                            @click="_chooseAvatar(item.url)"
                        ></el-image
                    ></el-col>
                </el-row>
            </el-card>
            <span slot="footer" class="dialog-footer">
                <el-button @click="avatarChooseBoxVisible = false"
                    >关 闭</el-button
                >
            </span>
        </el-dialog>
        <!-- 提供信息展示的抽屉 -->
        <el-drawer
            width="30%"
            size="70%"
            :visible.sync="infoVisible"
            direction="btt"
        >
            <h3 class="info_title">{{ infoTitle }}</h3>
            <el-card class="info_container">
                <div>
                    <el-empty
                        v-if="selectInfo === 3 && dataList.length === 0"
                        :description="'暂无' + infoTitle"
                    ></el-empty>
                    <el-empty
                        v-else-if="selectInfo === 2 && dataList.length === 0"
                        :description="'暂无' + infoTitle"
                    ></el-empty>
                    <div v-else>
                        <el-card v-for="(item, index) in dataList" :key="index">
                            <el-col :span="4">
                                <el-avatar
                                    @click.native="_toDetail(item.userId)"
                                    style="margin-top: -15px; cursor: pointer"
                                    :size="60"
                                    :src="item.userAvatar"
                                ></el-avatar>
                            </el-col>
                            <el-col :span="4">{{ item.nick }}</el-col>
                            <el-col :span="16">
                                <el-row>
                                    <el-col :span="4">收藏</el-col>
                                    <el-col :span="4">关注</el-col>
                                    <el-col :span="4">粉丝</el-col>
                                    <el-col :span="4">被赞</el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="4">{{
                                        item.collectionCounts
                                    }}</el-col>
                                    <el-col :span="4">{{
                                        item.followCounts
                                    }}</el-col>
                                    <el-col :span="4">{{
                                        item.fanCounts
                                    }}</el-col>
                                    <el-col :span="4">{{
                                        item.approvalCounts
                                    }}</el-col></el-row
                                >
                            </el-col>
                        </el-card>
                    </div>
                </div>
            </el-card>
        </el-drawer>
        <!-- 图片详情 -->
        <!-- dialogVisible：true:打开/false:关闭 -->
        <!-- photoId：传入图片id -->
        <!--  @close="photoDetailVisible = false" 必须否则有警告（el-dialog的警告）  -->
        <photo-detail
            :dialogVisible="photoDetailVisible"
            :photoId="photoId"
            @close="photoDetailVisible = false"
        />
    </div>
</template>



<script>
import userApi from '../request/userApi';
import avatarApi from '../request/avatarApi';
import fanApi from '../request/fanApi';
import imageApi from '../request/imageApi';
import commentApi from '../request/commentApi';
import PhotoDetail from '../component/PhotoDetail.vue';
import collectionApi from '../request/collectionApi';
export default {
    components: {
        PhotoDetail,
    },
    data() {
        return {
            dataList: [],
            selectInfo: 0,
            infoTitle: 'XXXXXXX',
            infoVisible: false,
            pageNum: 1,
            pages: 0,
            // 作品列表
            worksDetail: [],
            // 评论列表
            commentList: [],
            workShow: false,
            avatarChooseBoxVisible: false,
            userDetail: [],
            editForm: {
                userAvatar: '',
                nick: '',
                signature: '',
            },
            avatarList: [],
            editVisible: false,
            rules: {
                nick: [
                    {
                        required: true,
                        message: '请输入昵称',
                        trigger: 'blur',
                    },
                    {
                        min: 3,
                        max: 10,
                        message: '昵称长度在 3 到 10 个字符',
                        trigger: 'blur',
                    },
                ],
                signature: [
                    {
                        required: true,
                        message: '请输入个性签名',
                        trigger: 'blur',
                    },
                    {
                        min: 5,
                        max: 20,
                        message: '个性签名长度在 5 到 20 个字符',
                        trigger: 'blur',
                    },
                ],
            },
            // 图片详情
            photoDetailVisible: false,
            // 图片id
            photoId: null,
            // 收藏列表
            collectionList: [],
        };
    },
    methods: {
        _editUserInfo() {
            this.editForm.userAvatar = this.userDetail.userAvatar;
            this.editForm.nick = this.userDetail.nick;
            this.editForm.signature = this.userDetail.signature;
            this.editVisible = true;
        },
        _handleClose(done) {
            this.$confirm('确认关闭？')
                .then(() => {
                    done();
                })
                .catch(() => {});
        },
        _getUserInfo() {
            userApi.getUserInfo().then((res) => {
                console.log(res);
                if (res.code !== 200) {
                    this.$message.error('获取用户信息失败');
                    return;
                }
                this.userDetail = res.data;
            });
        },
        _firstGetUserInfo() {
            userApi.getUserInfo().then((res) => {
                console.log(res);
                if (res.code !== 200) {
                    this.$message.error('获取用户信息失败');
                    return;
                }
                this.userDetail = res.data;
                this._getPersonalPhotos();
            });
        },
        _changeAvatar() {
            avatarApi.getAllAvatar().then((res) => {
                this.avatarList = res.data;
                this.avatarChooseBoxVisible = true;
            });
        },
        _chooseAvatar(url) {
            this.editForm.userAvatar = url;
            this.avatarChooseBoxVisible = false;
        },
        _editUser() {
            this.$refs.editForm.validate((valid) => {
                if (!valid) {
                    return;
                }
                this.$confirm('确认修改？')
                    .then(() => {
                        userApi.editUser(this.editForm).then((res) => {
                            if (res.code !== 200) {
                                this.$message.error('修改失败');
                                return;
                            }
                            this.$message.success('修改成功');
                            this.editVisible = false;
                            this._getUserInfo();
                        });
                    })
                    .catch(() => {});
            });
        },
        _handleClick(tab) {
            // 切换tabs的页面，重置pageNum为1
            this.pageNum = 1;
            if (parseInt(tab.index) === 0) {
                this._getPersonalPhotos();
                return;
            }
            if (parseInt(tab.index) === 1) {
                this._getReplyList();
                return;
            }
            if (parseInt(tab.index) === 2) {
                this._getCollectionList();
                return;
            }
        },
        _showFollowrdInfo() {
            this.selectInfo = 2;
            this.infoTitle = '关注的人';
            fanApi.getFollowedList().then((res) => {
                this.dataList = res.data;
                this.infoVisible = true;
            });
        },
        _showFanInfo() {
            this.selectInfo = 3;
            this.infoTitle = '粉丝';
            fanApi.getFansList().then((res) => {
                this.dataList = res.data;
                this.infoVisible = true;
            });
        },
        // 前往个人详情页面
        _toDetail(id) {
            if (id === this.$store.state.userId) {
                this.$router.push('/personal');
                return;
            }
            this.$router.push({
                path: '/user/' + id,
            });
        },
        // 查询个人作品
        _getPersonalPhotos() {
            this.workShow = false;
            var data = {
                userid: this.userDetail.userId,
                pageNum: this.pageNum,
            };
            imageApi.getPhotoByUserId(data).then((res) => {
                if (res.code === 200) {
                    this.worksDetail = res.data.list;
                    this.pages = res.data.pages;
                    this.workShow = true;
                }
            });
        },

        // 前往上一页作品
        _toLastPhotos() {
            if (this.pageNum === 1) {
                this.$message.error('已经是第一页了');
                return;
            }
            this.pageNum--;
            this._getPersonalPhotos();
        },
        // 前往下一页作品
        _toNextPhotos() {
            if (this.pageNum === this.pages) {
                this.$message.error('已经是最后一页了');
                return;
            }
            this.pageNum++;
            this._getPersonalPhotos();
        },
        // 查询个人评论
        _getReplyList() {
            this.workShow = false;
            commentApi.getReplyList(this.pageNum).then((res) => {
                if (res.code === 200) {
                    this.commentList = res.data.list;
                    this.pages = res.data.pages;
                    this.workShow = true;
                }
            });
        },

        // 前往上一页评论
        _toLastReplyList() {
            if (this.pageNum === 1) {
                this.$message.error('已经是第一页了');
                return;
            }
            this.pageNum--;
            this._getReplyList();
        },
        // 前往下一页评论
        _toNextReplyList() {
            if (this.pageNum === this.pages) {
                this.$message.error('已经是最后一页了');
                return;
            }
            this.pageNum++;
            this._getReplyList();
        },
        // 删除图片
        _deleteWork(id) {
            this.$confirm('确认删除？')
                .then(() => {
                    imageApi.deleteImage(id).then((res) => {
                        if (res.code === 200) {
                            this.$message.success('删除成功');
                            this._getUserInfo();
                            // 查询个人作品
                            this._getPersonalPhotos();
                        }
                    });
                })
                .catch(() => {});
        },
        // 删除评论
        _deleteComment(id) {
            this.$confirm('确认删除？')
                .then(() => {
                    commentApi.deleteComment(id).then((res) => {
                        if (res.code === 200) {
                            this.$message.success('删除成功');
                            this._getReplyList();
                        }
                    });
                })
                .catch(() => {});
        },
        // 查看图片详情
        _openPhotoDetail(id) {
            console.log(id);
            this.photoDetailVisible = true;
            this.photoId = id;
        },
        // 取消收藏
        _cancelCollection(id) {
            this.$confirm('确认取消收藏？')
                .then(() => {
                    collectionApi.toggleCollect(id).then((res) => {
                        if (res.code === 401) {
                            this.$message.success('取消收藏成功');
                            this._getUserInfo();
                            // 查询个人收藏
                            this._getCollectionList();
                        }
                    });
                })
                .catch(() => {});
        },
        // 获取收藏列表
        _getCollectionList() {
            this.workShow = false;
            collectionApi.getCollectionList(this.pageNum).then((res) => {
                if (res.code === 200) {
                    this.collectionList = res.data.list;
                    this.pages = res.data.pages;
                    this.workShow = true;
                }
            });
        },
        // 前往上一页收藏
        _toLastCollectionList() {
            if (this.pageNum === 1) {
                this.$message.error('已经是第一页了');
                return;
            }
            this.pageNum--;
            this._getCollectionList();
        },
        // 前往下一页收藏
        _toNextCollectionList() {
            if (this.pageNum === this.pages) {
                this.$message.error('已经是最后一页了');
                return;
            }
            this.pageNum++;
            this._getCollectionList();
        },
    },
    created() {
        // 查询个人信息个人作品
        this._firstGetUserInfo();
        // 查询个人收藏
        this._getCollectionList();
        // 改变标签高亮状态
        this.$store.commit('changeSelectState', 6);
    },
};
</script>


<style scoped>
.imgbackground {
    background-image: url(../assets/img/R-C.jpg);
    height: 330px;
    width: 100%;
    text-align: center;
}
.imgbackground .avatar {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    margin-top: 40px;
}
.editBox .avatar {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    margin-top: 40px;
}
.describe {
    margin-top: 15px;
    font-size: 20px;
    font-weight: bold;
    cursor: pointer;
}
.describe :hover {
    color: #40ff96;
}
.describe_title {
    font-size: 30px;
    font-weight: bold;
    cursor: pointer;
}
.describe_title :hover {
    color: #ffaf40;
}
.userInfo {
    text-align: center;
}
.name {
    font-weight: bold;
}
.signature {
    cursor: pointer;
}
.signature :hover {
    color: #407cff;
}
.editBox {
    text-align: center;
}
.avatarContain {
    text-align: center;
    height: 300px;
    overflow-y: scroll;
}
.avataritem {
    width: 150px;
    height: 150px;
    cursor: pointer;
}
.container {
    height: 700px;
    width: 80%;
    margin-left: 10%;
    overflow-y: auto;
}
.transition-box {
    margin-bottom: 10px;
    width: 200px;
    height: 100px;
    border-radius: 4px;
    background-color: #409eff;
    text-align: center;
    color: #fff;
    padding: 40px 20px;
    box-sizing: border-box;
    margin-right: 20px;
}
.box-card {
    width: 90%;
    height: 260px;
    margin: 0 auto;
    margin-top: 20px;
    border-radius: 4px;
    /* text-align: center; */
    /* color: #fff; */
    /* padding: 40px 20px; */
    /* box-sizing: border-box; */
    /* margin-right: 20px; */
}
.image {
    width: 100%;
    height: 200px;
    border-radius: 4px;
}
.detailBox {
    width: 100%;
    display: none;
}
.image:hover .detailBox {
    display: block;
    width: 100%;
}
.info_container {
    margin-left: 15%;
    width: 70%;
    height: 300px;
    overflow-y: auto;
}
.info_title {
    text-align: center;
    margin-top: 10px;
}
.info_describe {
    font-size: 20px;
    margin: auto;
}
/* 我的评论 */
.comment_img {
    cursor: pointer;
    width: 160px;
    height: 100px;
    float: left;
    margin-bottom: 20px;
    border-radius: 5px;
}
.comment_title {
    float: left;
    margin-left: 30px;
    margin-top: 50px;
}
.comment_card {
    margin-top: 20px;
}
.comment_main {
    height: 100px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.comment_main span {
    margin: 0 30px;
}
.comment_delete {
    cursor: pointer;
}
</style>